package com.thiagov.poc;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Activity que permite a exibicao de uma pagina web para
 * dar informacoes de locais ao jogador.
 *
 * @author thiagov
 *
 */
public class InfoActivity extends Activity {

    /**
     * Classe que permite que a activity cuide de abrir URLs.
     * @author thiagov
     *
     */
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    private WebView mWebView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Seta o layout. */
        setContentView(R.layout.info);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);

        /* Escolhe a URL a ser utilizada. */
        String placeName = getIntent().getStringExtra("placeName");
        Cursor cursor = DroidTour.dh.getDatabase().query("places",
                new String[] {"infoLink"},
                "name=\"" + placeName  +"\"", null, null, null, null);
        cursor.moveToFirst();

        mWebView.loadUrl(cursor.getString(0));
        cursor.close();
    }

    /**
     * Permite que o usuario volte para a tela anterior ao clicar
     * em voltar.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
}
